package com.zzyl.redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Set;

@SpringBootTest
public class testSet {

    @Autowired
    private RedisTemplate<String,String> redisTemplate; // redisTemplate是springboot自动注入的


    /**
     * 操作集合类型的数据
     */
    @Test
    public void testSet(){
        //添加数据
        redisTemplate.opsForSet().add("myset","a","b","c","d");
        redisTemplate.opsForSet().add("myset2","a","b","x","y");
        System.out.println("===================================");
        //获取集合中的所有成员
        Set<String> members = redisTemplate.opsForSet().members("myset");
        System.out.println(members);
        System.out.println("===================================");
        //获取集合大小
        long size = redisTemplate.opsForSet().size("myset");
        System.out.println(size);
        System.out.println("===================================");
        //交集
        Set<String> intersection = redisTemplate.opsForSet().intersect("myset", "myset2");
        System.out.println("交集："+intersection);
        System.out.println("===================================");
        //并集
        Set<String> union = redisTemplate.opsForSet().union("myset", "myset2");
        System.out.println("并集："+union);
    }
}
